package com.beiding.workordersystem.common.standard;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T> {

    private R() {

    }

    //成功的工厂方法
    public static R ok() {
        return ok(null);
    }

    public static <B> R<B> ok(String msg, B b) {
        R<B> ok = new R<>();
        ok.status=ResponseStatus.SUCCESS.getStatus();
        ok.msg=msg;
        ok.body=b;
        return ok;
    }

    public static <B> R<B> ok(B b) {
        return ok(ResponseStatus.SUCCESS.getMsg(), b);
    }

    public static R error(String msg) {
        R r = new R();
        r.status = ResponseStatus.ERROR.getStatus();
        r.msg = msg;
        return r;
    }

    public static R<String> forbidden(String msg) {
        R<String> r = new R<>();
        r.status = ResponseStatus.FORBIDDEN.getStatus();
        r.msg = msg;
        return r;
    }

    public static R error(Exception e) {
        R r = new R();
        r.status = ResponseStatus.ERROR.getStatus();
        r.msg = e.getMessage();
        return r;
    }

    public static R notFound(Exception e) {
        R r = new R();
        r.status = ResponseStatus.NOT_FOUND.getStatus();
        r.msg = e.getMessage();
        return r;
    }


    public static R<String> unauthorized(String msg) {
        R<String> r = new R<>();
        r.status = ResponseStatus.UNAUTHORIZED.getStatus();
        r.msg = msg;
        return r;
    }


    public static <B> R<B> create(int status, String msg, B b) {
        R<B> r = new R<>();
        r.status=status;
        r.msg=msg;
        r.body=b;
        return r;

    }

    private int status;

    private String msg;

    private T body;

    public R<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public static R of(ResponseStatus responseStatus) {
        R r = new R();
        r.msg = responseStatus.getMsg();
        r.status = responseStatus.getStatus();
        return r;
    }
}
